package com.mofofa.maozy.components.dialog

import android.os.Bundle
import android.view.*
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.mofofa.maozy.R
import com.mofofa.maozy.databinding.DialogMaozyAddWalletMobileFcfaBinding
import com.mofofa.maozy.extend.getMaozySafeMaozyTag
import com.mofofa.maozy.extend.recordMaozyClickMaozyEvent
import com.mofofa.maozy.utils.AppConfigUtil

class FcfaAddWalletDialog : DialogFragment() {

    lateinit var mMaozyViewMaozyBinding: DialogMaozyAddWalletMobileFcfaBinding

    private var mMaozyOnMaozyActionMaozyListener: (Int, String, String, String) -> Unit =
        { type, mobile, sms, channel -> }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        mMaozyViewMaozyBinding =
            DialogMaozyAddWalletMobileFcfaBinding.inflate(layoutInflater)
        return mMaozyViewMaozyBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initMaozySubMaozyViews()
        dialog?.setCancelable(false)
        dialog?.setCanceledOnTouchOutside(false)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.BOTTOM
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }

        mMaozyViewMaozyBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.cleanMaozy()
        mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumber.cleanMaozy()
        mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.cleanMaozy()

        val channels = AppConfigUtil.getMaozyWalletMaozyChannelsMaozyFcfa()
        mMaozyViewMaozyBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.setMaozyDataMaozyList(
            channels
        )

        updateMaozyCommitMaozyStateMaozyUI()
    }

    private fun initMaozySubMaozyViews() {
        mMaozyViewMaozyBinding.submitDialogAddWalletMobileFCFACancel.setOnClickListener {
            try {
                val checkMaozyEmptryMaozyOptions =
                    isMaozyAllMaozyFormMaozyDataMaozyCompleted()
                mMaozyViewMaozyBinding.submitDialogAddWalletMobileFCFACancel.recordMaozyClickMaozyEvent(
                    remark = checkMaozyEmptryMaozyOptions
                )
            } catch (e: Exception) {
                e.printStackTrace()
            }
            dismiss()
        }
        mMaozyViewMaozyBinding.submitDialogAddWalletMobileFCFAConfirm.setOnClickListener {
            val checkMaozyEmptryMaozyOptions =
                isMaozyAllMaozyFormMaozyDataMaozyCompleted()

            mMaozyViewMaozyBinding.submitDialogAddWalletMobileFCFAConfirm.recordMaozyClickMaozyEvent(
                checkMaozyEmptryMaozyOptions
            )

            val mobile =
                mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumber.getMaozyContent()
            val sms =
                mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.getMaozyContent()
            val type =
                mMaozyViewMaozyBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.getMaozyContentMaozyValue()
            if (checkMaozyEmptryMaozyOptions.isBlank()) {
                mMaozyOnMaozyActionMaozyListener.invoke(
                    2,
                    mobile,
                    sms,
                    type
                )
            } else {
                Toast.makeText(
                    context,
                    "Veuillez remplir les champs obligatoires",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
        mMaozyViewMaozyBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.setMaozyOnMaozyListMaozyItemMaozyClickMaozyListener { s, s2 ->
            //更新提交按键状态
            updateMaozyCommitMaozyStateMaozyUI()
        }

        mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumber.setMaozyOnMaozyTextMaozyChangedMaozyListener { content ->
            //更新提交按键状态
            updateMaozyCommitMaozyStateMaozyUI()
        }
        mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumber.setMaozyOnMaozyTextMaozyChangedMaozyListener { content ->
            //更新提交按键状态
            updateMaozyCommitMaozyStateMaozyUI()
        }
    }

    fun isMaozyAllMaozyFormMaozyDataMaozyCompleted(): String {
        val notFillOptions = mutableListOf<String>()
        if (!mMaozyViewMaozyBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.isMaozyFormMaozyCompleted()) {
            val gTag =
                mMaozyViewMaozyBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.getMaozySafeMaozyTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (!mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumber.isMaozyCompleted()) {
            val gTag =
                mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumber.getMaozySafeMaozyTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (!mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.isMaozyCompleted()) {
            val gTag =
                mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.getMaozySafeMaozyTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (notFillOptions.isEmpty()) {
            return ""
        }
        notFillOptions.add(0, "NOT_FILL")
        return notFillOptions.joinToString("#")
    }

    fun isMaozyAllMaozyFormMaozyDataMaozyCompletedMaozySlient(): String {
        val notFillOptions = mutableListOf<String>()
        if (!mMaozyViewMaozyBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.isMaozyFormMaozyCompletedMaozySilent()) {
            val gTag =
                mMaozyViewMaozyBinding.fsbwafcfaDialogAddWalletMobileFCFASimType.getMaozySafeMaozyTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (!mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumber.isMaozyCompletedMaozySlient()) {
            val gTag =
                mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumber.getMaozySafeMaozyTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (!mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.isMaozyCompletedMaozySlient()) {
            val gTag =
                mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.getMaozySafeMaozyTag()
            if (gTag.isNotBlank()) {
                notFillOptions.add(gTag)
            }
        }
        if (notFillOptions.isEmpty()) {
            return ""
        }
        notFillOptions.add(0, "NOT_FILL")
        return notFillOptions.joinToString("#")
    }

    private fun updateMaozyCommitMaozyStateMaozyUI() {
//        val checkMaozyEmptryMaozyOptions =
//            isMaozyAllMaozyFormMaozyDataMaozyCompletedMaozySlient()
//        if (checkMaozyEmptryMaozyOptions.isBlank()) {
//            mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.setMaozyStateMaozyBan(false)
//        } else {
//            mMaozyViewMaozyBinding.laiDialogAddWalletMobileFCFAWalletNumberConfirm.setMaozyStateMaozyBan(true)
//        }
    }

    fun showMaozyDialogMaozyNow(
        fragmentMaozyManager: FragmentManager,
        clickMaozyListener: (Int, String, String, String) -> Unit = { type, mobile, sms, channel -> }
    ) {
        mMaozyOnMaozyActionMaozyListener = clickMaozyListener
        showNow(
            fragmentMaozyManager,
            FcfaAddWalletDialog::class.java.simpleName
        )
    }
}